import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const routes = [
  { path: '/login', name: 'login', component: () => import('@/pages/Login.vue'), meta: { hidden: true } },
  { path: '/register', name: 'register', component: () => import('@/pages/Register.vue'), meta: { hidden: true } },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/admin',
    component: () => import('@/layouts/AdminLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      { path: '', redirect: '/admin/dashboard' },
      { 
        path: 'dashboard', 
        name: 'dashboard', 
        component: () => import('@/pages/Dashboard.vue'), 
        meta: { 
          title: '仪表盘', 
          icon: 'HomeFilled',
          permission: 'dashboard'
        } 
      },
      { 
        path: 'inspection', 
        name: 'inspection', 
        component: () => import('@/pages/InspectionManagement.vue'), 
        meta: { 
          title: '巡检管理', 
          icon: 'List',
          permission: 'inspection'
        } 
      },
      { 
        path: 'inspection-new', 
        name: 'inspection-new', 
        component: () => import('@/pages/InspectionManagementNew.vue'), 
        meta: { 
          title: '巡检管理（新）', 
          icon: 'List',
          permission: 'inspection'
        } 
      },
      { 
        path: 'lines', 
        name: 'lines', 
        component: () => import('@/pages/LineManagement.vue'), 
        meta: { 
          title: '线路管理', 
          icon: 'Location',
          permission: 'lines'
        } 
      },
      { 
        path: 'defects', 
        name: 'defects', 
        component: () => import('@/pages/DefectManagement.vue'), 
        meta: { 
          title: '缺陷管理', 
          icon: 'Warning',
          permission: 'defects'
        } 
      },
      { 
        path: 'remedies', 
        name: 'remedies', 
        component: () => import('@/pages/RemedyManagement.vue'), 
        meta: { 
          title: '消缺管理', 
          icon: 'Tools',
          permission: 'remedies'
        } 
      },
      { 
        path: 'statistics', 
        name: 'statistics', 
        component: () => import('@/pages/Statistics.vue'), 
        meta: { 
          title: '统计', 
          icon: 'DataAnalysis',
          permission: 'statistics'
        } 
      },
      { 
        path: 'profile', 
        name: 'profile', 
        component: () => import('@/pages/Profile.vue'), 
        meta: { 
          title: '个人资料', 
          icon: 'User',
          permission: 'profile',
          hidden: true
        } 
      }
    ]
  },
  { path: '/:pathMatch(.*)*', redirect: '/login' }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

/**
 * 路由守卫
 */
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  
  // 检查token是否过期
  if (authStore.isLoggedIn && authStore.isTokenExpired()) {
    ElMessage.warning('登录已过期，请重新登录')
    next('/login')
    return
  }
  
  // 如果已登录且访问登录页或注册页，重定向到仪表盘
  if ((to.path === '/login' || to.path === '/register') && authStore.isLoggedIn) {
    next('/admin/dashboard')
    return
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    if (!authStore.isLoggedIn) {
      ElMessage.warning('请先登录')
      next('/login')
      return
    }
  }
  
  // 如果访问根路径且未登录，重定向到登录页
  if (to.path === '/' && !authStore.isLoggedIn) {
    next('/login')
    return
  }
  
  next()
})

export default router
